package com.aw.baselib.widgets

import android.app.Activity
import android.graphics.drawable.BitmapDrawable
import android.os.Build
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.PopupWindow
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.aw.baselib.R
import com.aw.baselib.adapter.CommonListBlueAdapter
import com.aw.baselib.base.AwBaseConstant
import com.aw.baselib.bean.CommonListBean
import com.aw.baselib.util.AwPopupwindowUtil
import com.aw.baselib.util.AwRecyclerViewUtil
import com.chad.library.adapter.base.BaseQuickAdapter

/**
 * Created by hzw
 */
class AwCommonListWithTitlePopupWindow(
    private val mActivity: Activity,
    title: String?,
    dismissListener: OnDismissListener?
) : PopupWindow(
    mActivity
) {
    private var mOnItemClickListener: OnItemClickListener? = null
    private val mAdapter: CommonListBlueAdapter
    private var mList: MutableList<out CommonListBean> = ArrayList()
    private val contentView: View
    fun setListData(list: List<out CommonListBean>) {
        mList = list.toMutableList()
        mAdapter.addAllData(mList)
    }

    init {
        contentView =
            View.inflate(mActivity, R.layout.view_popupwindow_common_list_with_title, null)
        val rcv_data = contentView.findViewById<View>(R.id.rcv_data) as RecyclerView
        val tv_cancel = contentView.findViewById<TextView>(R.id.tv_cancel)
        tv_cancel.setOnClickListener { dismiss() }
        val tv_title = contentView.findViewById<TextView>(R.id.tv_title)
        tv_title.text = title
        mAdapter = CommonListBlueAdapter()
        AwRecyclerViewUtil.setRecyclerViewLinearlayout(mActivity, rcv_data, mAdapter, true)
        mAdapter.onItemClickListener =
            BaseQuickAdapter.OnItemClickListener { adapter, view, position ->
                if (null != mOnItemClickListener) {
                    mOnItemClickListener!!.onClick(mList!![position])
                }
                dismiss()
            }
        setContentView(contentView)
        this.width = ViewGroup.LayoutParams.MATCH_PARENT
        this.height = ViewGroup.LayoutParams.WRAP_CONTENT
        this.animationStyle = R.style.sytle_anim_pop_in_out
        this.isFocusable = true
        this.isTouchable = true
        this.isOutsideTouchable = true
        setBackgroundDrawable(BitmapDrawable())
        this.update()
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            this.overlapAnchor = true
        }
        if (dismissListener == null) {
            setOnDismissListener {
                val lp = mActivity.window.attributes
                lp.alpha = AwBaseConstant.Popupwindow.POPUPWINDOW_NORMAL
                mActivity.window.attributes = lp
            }
        } else {
            setOnDismissListener(dismissListener)
        }
    }

    /**
     * 展示窗口
     */
    fun show(parent: View?) {
        val lp = mActivity.window.attributes
        lp.alpha = AwBaseConstant.Popupwindow.POPUPWINDOW_DARK
        mActivity.window.attributes = lp
        this.showAsDropDown(parent, 0, 0)
    }

    fun showAsLocation(parent: View) {
        val lp = mActivity.window.attributes
        lp.alpha = AwBaseConstant.Popupwindow.POPUPWINDOW_DARK
        mActivity.window.attributes = lp
        val windowPos = AwPopupwindowUtil.calculatePopWindowPos(parent, contentView)
        val xOff = 20 // 可以自己调整偏移
        windowPos[0] -= xOff
        showAtLocation(parent, Gravity.TOP or Gravity.START, windowPos[0], windowPos[1])
    }

    fun showAsLocationNoAlpha(parent: View) {
        val windowPos = AwPopupwindowUtil.calculatePopWindowPos(parent, contentView)
        val xOff = 20 // 可以自己调整偏移
        windowPos[0] -= xOff
        showAtLocation(parent, Gravity.TOP or Gravity.START, windowPos[0], windowPos[1])
    }

    fun showAsLocationBottom(parent: View?) {
        val lp = mActivity.window.attributes
        lp.alpha = AwBaseConstant.Popupwindow.POPUPWINDOW_DARK
        mActivity.window.attributes = lp
        showAtLocation(parent, Gravity.BOTTOM or Gravity.CENTER_HORIZONTAL, 0, 0)
    }

    /**
     * calendar菜单选择监听接口
     */
    interface OnItemClickListener {
        fun onClick(bean: Any?)
    }

    /**
     * 设置菜单监听
     */
    fun setOnItemClickListener(listener: OnItemClickListener?) {
        mOnItemClickListener = listener
    }
}